package io.renren.wechat.config;

import io.renren.wechat.common.aes.WXBizCmdCrypt;
import io.renren.wechat.common.aes.WXBizCorpCrypt;
import io.renren.wechat.common.aes.WXBizMsgCrypt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author dmcp@qq.com
 * @since 2021/11/14 20:40
 */
@Configuration
@EnableConfigurationProperties(io.renren.wechat.config.WechatProperties.class)
public class WechatConfiguration {
    private static final Logger LOGGER = LoggerFactory.getLogger(WechatConfiguration.class);
    private WechatProperties wechatProperties;

    @Bean
    public WXBizCorpCrypt corp() throws Exception {
        LOGGER.info("using WechatProperties: {}", wechatProperties);

        return new WXBizCorpCrypt(
                wechatProperties.getCorpToken(),
                wechatProperties.getCorpEncodingAESKey(),
                wechatProperties.getCorpId());
    }

    @Bean
    public WXBizCmdCrypt cmd() throws Exception {
        LOGGER.info("using WechatProperties: {}", wechatProperties);

        return new WXBizCmdCrypt(
                wechatProperties.getSuiteToken(),
                wechatProperties.getSuiteEncodingAESKey(),
                wechatProperties.getCorpId());
    }

    @Bean
    public WXBizMsgCrypt data() throws Exception {
        LOGGER.info("using WechatProperties: {}", wechatProperties);

        return new WXBizMsgCrypt(
                wechatProperties.getSuiteToken(),
                wechatProperties.getSuiteEncodingAESKey(),
                wechatProperties.getSuiteId());
    }

    @Autowired
    public void setWechatProperties(WechatProperties wechatProperties) {
        this.wechatProperties = wechatProperties;
    }
}
